//
//  ViewController.m
//  24-homework-加密解密
//
//  Created by 王道林 on 16/8/16.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"
#import "NSString+WDLMD5.h"
#import "WHSNetworkParser.h"
#import "AFNetworking.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textFieldAdd;

@property (weak, nonatomic) IBOutlet UITextField *textFieldAnswer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)buttonMD5:(id)sender {
    
    _textFieldAnswer.text = [_textFieldAnswer.text stringWith16BitMD5Upper];
    
    
}

- (IBAction)buttonAdd:(id)sender {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postUrlString = @"http://tool.zzblo.com/api/md5/encrypt";
    NSString *parameters = [NSString stringWithFormat:@"text=%@", _textFieldAdd.text];
    
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    
    
    [manager POST:postUrlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"加密成功: %@", responseObject);
        _textFieldAnswer.text = responseObject[@"secret16"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];

    
    
    
}


- (IBAction)buttonDes:(id)sender {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postUrlString = @"http://tool.zzblo.com/api/md5/decrypt";
    NSString *parameters = [NSString stringWithFormat:@"secret=%@", _textFieldAnswer.text];
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    [manager POST:postUrlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"解密成功: %@", responseObject);
        _textFieldAdd.text = responseObject[@"text"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];

    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
